package com.me.myHungTrung;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;

public class GameScreen extends Stage implements Screen{
	GameController game;
	public static int screenWidth, screenHeight;
	public static Array<Chick> chicks;
	public static Array<Egg> eggs;
	public Bag bag;
	TextureRegion Background, help;
	public BitmapFont largeFont, midFont, specFont;
	
	public static boolean isGAMEOVER, isShowHelp;

	public GameScreen(GameController _game, int width, int height){
		game = _game;
		screenWidth = width;
		screenHeight = height;
		
		chicks = new Array<Chick>();	
		addChick(Chick.GA);
		addChick(Chick.VIT);
		addChick(Chick.NGAN);
		
		eggs = new Array<Egg>();
		largeFont = Assets.largeFont;
		midFont = Assets.midFont;
		specFont = Assets.specFont;
		Background = Assets.background;
		help = Assets.help;
		bag = new Bag();
	}
	
	public void addChick(int kind){
		chicks.add(new Chick(kind));
	}
	
	public static void addEgg(Egg egg){
		eggs.add(egg);
	}
	
	public static void removeEgg(Egg egg){
		eggs.removeValue(egg, true);
	}
	
	@Override
	public void render(float delta) {
//		super.draw();
		if(isGAMEOVER || isShowHelp){
			//
		}else
			update(delta);
		draw(this.getSpriteBatch());	
	}
	
	public void update(float delta){
		//Update trang thai cho ga, vit, ngan
		for(Chick obj : chicks){
			((Chick)obj).update(delta);
		}
		
		//Update trang thai cho trung, phan
		for(Egg obj : eggs){
			((Egg)obj).update(delta);
		}
		
		//Update trang thai cho ro
		bag.update(eggs);
		
		if(PlayerInfo.NumOfEggs >= 20){//Neu du 20 qua trung thi cong them speed
			Egg.speedY += 0.5f;
			PlayerInfo.NumOfEggs = 0;
			PlayerInfo.Level++;
		}
		
		if(PlayerInfo.Life == 0){//Game Over
			isGAMEOVER = true;
		}
	}
	
	ShapeRenderer shapeRenderer = new ShapeRenderer();
	public void draw(SpriteBatch batch){	      
		batch.begin();
		batch.draw(Background, 0, 0);
		//Ve trung/phan
		for(Egg obj : eggs){
			((Egg)obj).draw(batch);
		}
		
		//Ve ga,vit,ngan
		for(Chick obj : chicks){
			((Chick)obj).draw(batch);
		}
		
		//ve ro
		bag.draw(batch);
		
		//Ve info
		largeFont.setColor(Color.RED);
		largeFont.draw(batch, "LIFE: " + PlayerInfo.Life, screenWidth - 120, screenHeight - 10);		
		largeFont.setColor(Color.GREEN);
		largeFont.draw(batch, "LEVEL: " + PlayerInfo.Level, 10, screenHeight - 10);
		largeFont.setColor(Color.BLUE);
		largeFont.draw(batch, "SCORE: " + PlayerInfo.Score, 10, 40);
		largeFont.setColor(Color.MAGENTA);
		largeFont.draw(batch, "Help", screenWidth - 80, 40);
		
		if(isGAMEOVER){
			specFont.draw(batch, "GAME OVER", screenWidth/2 - 120, screenHeight/2);
			midFont.draw(batch, "Touch to reset", screenWidth/2 - 50, screenHeight/2 - 50);
		}
		
		if(isShowHelp){
			batch.draw(help, (screenWidth - help.getRegionWidth())/2, (screenHeight - help.getRegionHeight())/2);
		}
		
		batch.end();
		
		//draw line
		shapeRenderer.setColor(0.5f,0.5f,0.5f,0.5f);	      
	    shapeRenderer.begin(ShapeType.Line);
	    shapeRenderer.line(0, screenHeight - screenHeight/6, screenWidth, screenHeight - screenHeight/6);	            
	    shapeRenderer.line(0, screenHeight/12, screenWidth,screenHeight/12);	            
	    shapeRenderer.end();
	}
	
	public void ResetGame(){
		isGAMEOVER = false;
		PlayerInfo.reset();
		chicks.clear();	
		addChick(Chick.GA);
		addChick(Chick.VIT);
		addChick(Chick.NGAN);
		
		eggs.clear();
		bag.setX(screenWidth/2);
		Egg.speedY = 1;
	}
	
	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		if(isGAMEOVER){
			ResetGame();
			return true;
		}
		if(isShowHelp){
			isShowHelp = false;
			return true;
		}
		if(screenX > screenWidth - 80 && screenHeight - screenY < 40)
			isShowHelp = true;
		if(screenY < screenHeight/6)
			addChick(MathUtils.random(0, 2));
		bag.setX(screenX);
		 
		return super.touchDown(screenX, screenY, pointer, button);
		
	}	
	
	@Override
	   public boolean keyDown(int keycode) {
    		if(isGAMEOVER)
    			ResetGame();
			
	        if(keycode == Keys.BACK || keycode == Keys.ESCAPE){ 
	        	if(isShowHelp)
	        		isShowHelp = false;
	        	else
	        		game.ExitGame();
	        }else if(keycode == Keys.HOME){
	        	game.ExitGame();	        
	        }else if(keycode == Keys.LEFT){
	        	if(isShowHelp)
	        		isShowHelp = false;
	        	bag.move(Bag.BAG_LEFT);
	        }else if(keycode == Keys.RIGHT){
	        	if(isShowHelp)
	        		isShowHelp = false;
	        	bag.move(Bag.BAG_RIGHT);
	        }else if(keycode == Keys.G){
	        	addChick(Chick.GA);
	        }else if(keycode == Keys.V){
	        	addChick(Chick.VIT);
	        }else if(keycode == Keys.N){
	        	addChick(Chick.NGAN);
	        }else if(keycode == Keys.SPACE){
	        	isShowHelp = !isShowHelp;
	        }
	        return false;              
	   }
	
	@Override
	public void dispose() {
		super.dispose();
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}
	
}
